package org.dromara.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.constant.MachineConstant;
import org.dromara.system.domain.SimUserAnnouncement;
import org.dromara.system.domain.bo.SimUserAnnouncementBo;
import org.dromara.system.domain.vo.SimUserAnnouncementVo;
import org.dromara.system.mapper.SimUserAnnouncementMapper;
import org.dromara.system.service.ISimUserAnnouncementService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 首页轮播公告Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-01
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SimUserAnnouncementServiceImpl extends ServiceImpl<SimUserAnnouncementMapper, SimUserAnnouncement> implements ISimUserAnnouncementService {


    /**
     * 查询首页轮播公告
     *
     * @param id 主键
     * @return 首页轮播公告
     */
    @Override
    public SimUserAnnouncementVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询首页轮播公告列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 首页轮播公告分页列表
     */
    @Override
    public TableDataInfo<SimUserAnnouncementVo> queryPageList(SimUserAnnouncementBo bo, PageQuery pageQuery) {
        IPage<SimUserAnnouncementVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<SimUserAnnouncementVo> list = baseMapper.findAll(page, bo);
        return TableDataInfo.build(list);
    }


    @Override
    public SimUserAnnouncementVo querySimUserAnnouncement() {
        SimUserAnnouncement simUserAnnouncement = lambdaQuery()
            .eq(SimUserAnnouncement::getStatus, MachineConstant.NUMBER.ONE)
            .orderByDesc(SimUserAnnouncement::getCreateTime)
            .last("limit 1").one();
        return MapstructUtils.convert(simUserAnnouncement, SimUserAnnouncementVo.class);
    }

    /**
     * 查询符合条件的首页轮播公告列表
     *
     * @param bo 查询条件
     * @return 首页轮播公告列表
     */
    @Override
    public List<SimUserAnnouncementVo> queryList(SimUserAnnouncementBo bo) {
        LambdaQueryWrapper<SimUserAnnouncement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SimUserAnnouncement> buildQueryWrapper(SimUserAnnouncementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SimUserAnnouncement> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SimUserAnnouncement::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getDetails()), SimUserAnnouncement::getDetails, bo.getDetails());
        lqw.eq(bo.getStatus() != null, SimUserAnnouncement::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增首页轮播公告
     *
     * @param bo 首页轮播公告
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SimUserAnnouncementBo bo) {
        SimUserAnnouncement add = MapstructUtils.convert(bo, SimUserAnnouncement.class);
        if (ObjectUtil.equal(add.getStatus(), MachineConstant.NUMBER.ONE)) {
            //把其他的变成不启用
            lambdaUpdate()
                .set(SimUserAnnouncement::getStatus, MachineConstant.NUMBER.ZERO)
                .eq(SimUserAnnouncement::getDelFlag, '0')
                .update();
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改首页轮播公告
     *
     * @param bo 首页轮播公告
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SimUserAnnouncementBo bo) {
        SimUserAnnouncement update = MapstructUtils.convert(bo, SimUserAnnouncement.class);
        if (ObjectUtil.equal(update.getStatus(), MachineConstant.NUMBER.ONE)) {
            //把其他的变成不启用
            lambdaUpdate()
                .set(SimUserAnnouncement::getStatus, MachineConstant.NUMBER.ZERO)
                .ne(SimUserAnnouncement::getId, update.getId())
                .update();
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */


    /**
     * 校验并批量删除首页轮播公告信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


}
